﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using MaterialDesignThemes.Wpf;
using Lhn.Ds.Core.CarDevice;
using Lhn.Ds.Core.Exceptions;
using Lhn.Ds.Core.Service;

namespace Lhn.Ds.Core.Controll
{
    public class CancleCardModel : BaseModel
    {
        private CardInfoModel _cardInfo;

        public CardInfoModel CardInfo
        {
            get => _cardInfo;
            set
            {
                if (_cardInfo == value) return;
                _cardInfo = value;
                OnPropertyChanged();
            }
        }

        [Import] public UsbCardService UsbCardService { get; set; }
        [Import] public CardService CardService { get; set; }

        public ICommand SubmitCommand =>
            new ReplayCommand(async o => { await DialogHost.Show(o, ClosingEventHandler); },
                () => !string.IsNullOrWhiteSpace(CardInfo?.Numer));

        public async void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
        {
            if (string.Equals(eventArgs.Parameter, "true"))
            {
                try
                {
                    UsbCardService.ResetCard(CardInfo);
                    if (CardInfo != null)
                    {
                        var remoteCard = await CardService.GetCard(CardInfo.Code);
                        if (remoteCard != null)
                            await CardService.DeleteCard(remoteCard.Id);
                    }

                    if (CardInfo != null)
                    {
                        DispatchUI(() => { CardInfo.Numer = ""; });
                        DsContext.InstanceData.SnackbarMessageQueue.Enqueue($"卡号{CardInfo.Numer}销卡成功");
                    }
                    else
                    {
                        DsContext.InstanceData.SnackbarMessageQueue.Enqueue($"强制销卡成功");
                    }
                    UsbCardService.Beep();
                }
                catch (PasswordException)
                {
                    var e = new SytechException("这是一张空卡");
                    DsContext.InstanceData.ProccessException(e);
                }
                catch (Exception e)
                {
                    if(CardInfo!=null)UsbCardService.WriteCard(CardInfo);
                    DsContext.InstanceData.ProccessException(e is SytechException?e:new SysException(e));
                }
            }
        }

        public ICommand ReadCardCommand => new ReplayCommand(o =>
        {
            Dispatch(() =>
            {
                try
                {
                    UsbCardService.SelectCard();
                    var rt = UsbCardService.ReadCard();

                    DispatchUI(() => { CardInfo = rt; });
                    UsbCardService.Beep();
                }
                catch (Exception e)
                {
                    DispatchUI(() =>
                    {
                        if (CardInfo != null) CardInfo.Numer = "";
                    });
                    DsContext.InstanceData.ProccessException(e);
                }
                finally
                {
                    DispatchUI(CommandManager.InvalidateRequerySuggested);
                }
            });
        });
    }
}